/**
 * 
 */
package com.msoe.client.panels;

import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.MapTypeControl;
import com.google.gwt.maps.client.control.SmallMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.msoe.client.Session;
import com.msoe.client.panels.eventhandlers.AddRideHandler;
import com.msoe.client.customwidgets.NavigationMenu;

public class AddRide implements MSOERPanel {

	public static final String DIV_ID = "add_ride";

	private Session session;
	private Panel addRidePanel;
	private TextBox T_map_input;
	private MapWidget mapWiget;
	private LatLng DEFAULT_CENTER_LATLNG = LatLng.newInstance(43.045323,-87.907641);
	private int DEFAULT_ZOOM_LEVEL = 13;
	private AddRideHandler handler;

	public AddRide(Session session){
		this.session = session;
		handler = new AddRideHandler(this);

		addRidePanel = new VerticalPanel();

		NavigationMenu navMenu = new NavigationMenu(this);
		HorizontalPanel MapPanel = new HorizontalPanel();
		HorizontalPanel mapInputPanel = new HorizontalPanel();

		//create the button and add listener
		Button B_map_input = new Button("Map Destination");
		B_map_input.addClickListener(handler);

		Label L_map_input = new Label("Destination:");
		T_map_input = new TextBox();

		mapInputPanel.add(L_map_input);
		mapInputPanel.add(T_map_input);
		mapInputPanel.add(B_map_input);

		mapWiget = new MapWidget(DEFAULT_CENTER_LATLNG, 13);  
		mapWiget.setSize("1000px", "600px");  

		//set default zoom level
		DEFAULT_ZOOM_LEVEL = mapWiget.getZoomLevel();

		mapWiget.addControl(new SmallMapControl());  
		mapWiget.addControl(new MapTypeControl()); 

		MapPanel.add(mapWiget);

		addRidePanel.add(navMenu);
		addRidePanel.add(mapInputPanel);
		addRidePanel.add(MapPanel);
	}

	public void show() {
		if(session.validate()){
			clear();
			RootPanel.get().clear();
			RootPanel.get(DIV_ID).add(addRidePanel);
		}
		else{
			PanelMap.getPanel("Login").show();
		}
	}

	public void hide(){
		addRidePanel.removeFromParent();
	}

	private void clear(){
		T_map_input.setText("");
		mapWiget.clearOverlays();
		mapWiget.setCenter(DEFAULT_CENTER_LATLNG);
		mapWiget.setZoomLevel(DEFAULT_ZOOM_LEVEL);
	}

	public MapWidget getMapWidget(){
		return mapWiget;
	}
	
	public String getDestination(){
		return T_map_input.getText();
	}

	public Session getSession() {
		return session;
	}
	
}
